﻿// ***********************************************************
// 工位基本图操作类
// Creator:yehui  Date:2013-02-19
// ***********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VipSys.SystemEntity;
using System.Data;
using VipSys.Common.Library;
using System.Diagnostics;

namespace VipSys.DataLayer
{
    public class PicStationAccess
    {
        private static PicStationAccess m_Instance = null;
        /// <summary>
        /// 单实例
        /// </summary>
        public static PicStationAccess Instance
        {
            get
            {
                if (m_Instance == null)
                    m_Instance = new PicStationAccess();
                return m_Instance;
            }
        }

        public short Add(PicStation picstation)
        {
            string szFiled = string.Format("{0},{1},{2},{3},{4},{5},{6}"
                 , SystemData.PicStationTable.Name
                , SystemData.PicStationTable.PicNormal
                , SystemData.PicStationTable.PicOccupy
                , SystemData.PicStationTable.PicOrder
                , SystemData.PicStationTable.Flag
                , SystemData.PicStationTable.Width
                , SystemData.PicStationTable.Height);
            string szValue = string.Format("'{0}','{1}','{2}','{3}','{4}',{5},{6}"
                  , picstation.Name
                , picstation.PicNormal
                , picstation.PicOccupy
                , picstation.PicOrder
                , picstation.Flag
                , picstation.Width
                , picstation.Height);
            string szSql = string.Format("Insert Into {0}({1}) values({2})"
                , SystemData.DBTable.PicStation
                , szFiled
                , szValue);
            try
            {
                int result = SystemParam.Instance.DBAccess.ExecuteNonQuery(szSql);
                if (result <= 0)
                    return SystemData.ReturnValue.EXCEPTION;
                return SystemData.ReturnValue.OK;
            }
            catch (Exception ex)
            {
                StackTrace st = new StackTrace(new StackFrame(true));
                StackFrame sf = st.GetFrame(0);
                SystemParam.Instance.Log.Error(string.Format("{0}\nErrorInfo:{1}", sf.GetMethod(), ex.ToString()));
                return SystemData.ReturnValue.EXCEPTION;
            }
        }

        public short Edit(PicStation picsation)
        {
            string szFiledValue = string.Format("{0}='{1}',{2}='{3}',{4}='{5}',{6}='{7}',{8}='{9}',{10}={11},{12}={13}"
                , SystemData.PicStationTable.Name, picsation.Name
                , SystemData.PicStationTable.PicNormal, picsation.PicNormal
                , SystemData.PicStationTable.PicOccupy, picsation.PicOccupy
                , SystemData.PicStationTable.PicOrder, picsation.PicOrder
                , SystemData.PicStationTable.Flag, picsation.Flag
                , SystemData.PicStationTable.Width, picsation.Width
                , SystemData.PicStationTable.Height, picsation.Height);
            string szCondition = string.Format(" {0}={1}"
                , SystemData.PicStationTable.ID, picsation.ID);
            string szSql = string.Format("update {0} set {1} where {2}"
                , SystemData.DBTable.PicStation
                , szFiledValue
                , szCondition);
            try
            {
                int result = SystemParam.Instance.DBAccess.ExecuteNonQuery(szSql);
                if (result <= 0)
                    return SystemData.ReturnValue.EXCEPTION;
                return SystemData.ReturnValue.OK;
            }
            catch (Exception ex)
            {
                StackTrace st = new StackTrace(new StackFrame(true));
                StackFrame sf = st.GetFrame(0);
                SystemParam.Instance.Log.Error(string.Format("{0}\nErrorInfo:{1}", sf.GetMethod(), ex.ToString()));
                return SystemData.ReturnValue.EXCEPTION;
            }
        }

        public short Del(PicStation picstation)
        {
            string szCondition = string.Format(" {0}={1}"
                , SystemData.PicStationTable.ID, picstation.ID);
        
            string szSql = string.Format("Delete from {0}  where {1}"
                , SystemData.DBTable.PicStation
                , szCondition);
            try
            {
                int result = SystemParam.Instance.DBAccess.ExecuteNonQuery(szSql);
                if (result <= 0)
                    return SystemData.ReturnValue.EXCEPTION;
                return SystemData.ReturnValue.OK;
            }
            catch (Exception ex)
            {
                StackTrace st = new StackTrace(new StackFrame(true));
                StackFrame sf = st.GetFrame(0);
                SystemParam.Instance.Log.Error(string.Format("{0}\nErrorInfo:{1}", sf.GetMethod(), ex.ToString()));
                return SystemData.ReturnValue.EXCEPTION;
            }
        }

      

        public short GetPicStationList(PicStation query, ref List<PicStation> lstPicStation)
        {
            if (lstPicStation == null)
                lstPicStation = new List<PicStation>();
            lstPicStation.Clear();
            string szField = string.Format("{0},{1},{2},{3},{4},{5},{6},{7}"
                , SystemData.PicStationTable.ID
                , SystemData.PicStationTable.Name
                , SystemData.PicStationTable.PicNormal
                , SystemData.PicStationTable.PicOccupy
                , SystemData.PicStationTable.PicOrder
                , SystemData.PicStationTable.Flag
                , SystemData.PicStationTable.Width
                , SystemData.PicStationTable.Height);
            string szCondition=string.Format("1=1");
           
            string szSql=string.Format("select {0} from {1} where {2}"
                , szField
                , SystemData.DBTable.PicStation
                , szCondition);

            try
            {
                DataSet ds=SystemParam.Instance.DBAccess.ExecuteQuery(szSql);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    PicStation picstation = new PicStation();
                    picstation.ID = int.Parse(ds.Tables[0].Rows[i][SystemData.PicStationTable.ID].ToString());
                    picstation.PicNormal = ds.Tables[0].Rows[i][SystemData.PicStationTable.PicNormal].ToString();
                    picstation.PicOccupy = ds.Tables[0].Rows[i][SystemData.PicStationTable.PicOccupy].ToString();
                    picstation.PicOrder = ds.Tables[0].Rows[i][SystemData.PicStationTable.PicOrder].ToString();
                    picstation.Flag = ds.Tables[0].Rows[i][SystemData.PicStationTable.Flag].ToString();
                    picstation.Width = ds.Tables[0].Rows[i][SystemData.PicStationTable.Width].ToString();
                    picstation.Height = ds.Tables[0].Rows[i][SystemData.PicStationTable.Height].ToString();
                   
                    lstPicStation.Add(picstation);
                }
                return SystemData.ReturnValue.OK;
            }
            catch (Exception ex)
            {
                StackTrace st = new StackTrace(new StackFrame(true));
                StackFrame sf = st.GetFrame(0);
                SystemParam.Instance.Log.Error(string.Format("{0}\nErrorInfo:{1}", sf.GetMethod(), ex.ToString()));
                return SystemData.ReturnValue.EXCEPTION;
            }
        }
        public short GetPicStationPageList(PicStation query, ref List<PicStation> lstPicStation)
        {
            if (lstPicStation == null)
                lstPicStation = new List<PicStation>();
            lstPicStation.Clear();
            StringBuilder sbSql = new StringBuilder();
            sbSql.AppendFormat("SELECT TOP {0} * FROM {1}"
                , query.Limit
                , SystemData.DBTable.PicStation
                );
            if (query.Start != "0")
            {
                sbSql.Append(" WHERE (ID <");
                sbSql.AppendFormat(" (SELECT min(ID) FROM (SELECT TOP {0} id  FROM {1} ORDER BY ID Desc) AS T)"
                    , query.Start
                    , SystemData.DBTable.PicStation
                    );
                sbSql.Append(" )");
            }
            sbSql.Append(" ORDER BY ID Desc");
            try
            {
                DataSet ds = SystemParam.Instance.DBAccess.ExecuteQuery(sbSql.ToString());
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    PicStation picstation = new PicStation();
                    picstation.ID = int.Parse(ds.Tables[0].Rows[i][SystemData.PicStationTable.ID].ToString());
                    picstation.Name = ds.Tables[0].Rows[i][SystemData.PicStationTable.Name].ToString();
                    picstation.PicNormal = ds.Tables[0].Rows[i][SystemData.PicStationTable.PicNormal].ToString();
                    picstation.PicOccupy = ds.Tables[0].Rows[i][SystemData.PicStationTable.PicOccupy].ToString();
                    picstation.PicOrder = ds.Tables[0].Rows[i][SystemData.PicStationTable.PicOrder].ToString();
                    picstation.Flag = ds.Tables[0].Rows[i][SystemData.PicStationTable.Flag].ToString();
                    picstation.Width = ds.Tables[0].Rows[i][SystemData.PicStationTable.Width].ToString();
                    picstation.Height = ds.Tables[0].Rows[i][SystemData.PicStationTable.Height].ToString();
                  
                    lstPicStation.Add(picstation);
                }
                return SystemData.ReturnValue.OK;
            }
            catch (Exception ex)
            {
                StackTrace st = new StackTrace(new StackFrame(true));
                StackFrame sf = st.GetFrame(0);
                SystemParam.Instance.Log.Error(string.Format("{0}\nErrorInfo:{1}", sf.GetMethod(), ex.ToString()));
                return SystemData.ReturnValue.EXCEPTION;
            }
        }
        public short GetTotalCount(PicStation query, ref int totalCount)
        {
            string szCondition = string.Format("1=1 ");
            string szSql = string.Format("select count(*) from {0} where {1}"
                , SystemData.DBTable.PicStation
                , szCondition);
            try
            {
                DataSet ds = SystemParam.Instance.DBAccess.ExecuteQuery(szSql);
                totalCount = int.Parse(ds.Tables[0].Rows[0][0].ToString());
                return SystemData.ReturnValue.OK;
            }
            catch (Exception ex)
            {
                StackTrace st = new StackTrace(new StackFrame(true));
                StackFrame sf = st.GetFrame(0);
                SystemParam.Instance.Log.Error(string.Format("{0}\nErrorInfo:{1}", sf.GetMethod(), ex.ToString()));
                return SystemData.ReturnValue.EXCEPTION;
            }
        }
        public short GetPicStation(PicStation query, ref PicStation picstation)
        {
            string szField = string.Format("{0},{1},{2},{3},{4},{5}"
               , SystemData.PicStationTable.ID
                , SystemData.PicStationTable.Name
               , SystemData.PicStationTable.PicNormal
               , SystemData.PicStationTable.PicOccupy
               , SystemData.PicStationTable.PicOrder
               , SystemData.PicStationTable.Flag
               ,SystemData.PicStationTable.Width
               , SystemData.PicStationTable.Height);
            string szCondition = string.Format("1=1");

            if (query.ID != 0)
                szCondition = string.Format("{0} and {1}={2}"
                    , szCondition
                    , SystemData.PicStationTable.ID, query.ID.ToString());
            string szSql = string.Format("select {0} from {1} where {2}"
                , szField
                , SystemData.DBTable.PicStation
                , szCondition);
            try
            {
                DataSet ds = SystemParam.Instance.DBAccess.ExecuteQuery(szSql);
                if (ds == null || ds.Tables[0].Rows.Count <= 0)
                    return SystemData.ReturnValue.RES_NO_FOUND;
                picstation.ID = int.Parse(ds.Tables[0].Rows[0][SystemData.PicStationTable.ID].ToString());
                picstation.Name = ds.Tables[0].Rows[0][SystemData.PicStationTable.Name].ToString();
                picstation.PicNormal = ds.Tables[0].Rows[0][SystemData.PicStationTable.PicNormal].ToString();
                picstation.PicOccupy = ds.Tables[0].Rows[0][SystemData.PicStationTable.PicOccupy].ToString();
                picstation.PicOrder = ds.Tables[0].Rows[0][SystemData.PicStationTable.PicOrder].ToString();
                picstation.Flag = ds.Tables[0].Rows[0][SystemData.PicStationTable.Flag].ToString();
                picstation.Width = ds.Tables[0].Rows[0][SystemData.PicStationTable.Width].ToString();
                picstation.Height = ds.Tables[0].Rows[0][SystemData.PicStationTable.Height].ToString();
                  
                return SystemData.ReturnValue.OK;
            }
            catch (Exception ex)
            {
                StackTrace st = new StackTrace(new StackFrame(true));
                StackFrame sf = st.GetFrame(0);
                SystemParam.Instance.Log.Error(string.Format("{0}\nErrorInfo:{1}", sf.GetMethod(), ex.ToString()));
                return SystemData.ReturnValue.EXCEPTION;
            }
        }
    }
}
